package net.ojbk.dao;
import java.sql.*;

import net.ojbk.database.DatabaseConnection;
import net.ojbk.po.MessageBoard;

public class MessageBoardDao {
	static final String tableName="messageBoard";
	DatabaseConnection dbconn;
	Statement stmt;
	public MessageBoardDao(){
		dbconn=new DatabaseConnection();
		stmt=dbconn.getStatement();
	}
	public void finalize(){
		dbconn.close();
	}
	public boolean addOneRecord(MessageBoard mb){
		boolean flag=false;
		try {
			String sql="insert into "
					+ tableName
					+ "(ts,title,content) value('"
					+ mb.getTs()
					+ "','"
					+ mb.getTitle()
					+ "','"
					+ mb.getContent()
					+ "')";
			stmt.executeUpdate(sql);
			flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public boolean deleteOneRecord(MessageBoard mb){
		boolean flag=false;
		try {
			String sql="delete from "
					+ tableName
					+ " where ts='"
					+ mb.getTs()
					+ "'";
			stmt.executeUpdate(sql);
			flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public boolean updateOneRecord(MessageBoard mb){
		boolean flag=false;
		try {
			String sql="update "
					+ tableName
					+ " set title='"
					+ mb.getTitle()
					+ "', content='"
					+ mb.getContent()
					+ "' where ts='"
					+ mb.getTs()
					+ "'";
			stmt.executeUpdate(sql);
			flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public boolean queryOneRecord(MessageBoard mb){
		boolean flag=false;
		ResultSet re;
		try {
			String sql="select * from "
					+ tableName
					+ " where ts='"
					+ mb.getTs()
					+ "'";
			re=stmt.executeQuery(sql);
			if(re.next()){
				mb.setTs(re.getTimestamp("Ts"));
				mb.setContent(re.getString("content"));
				mb.setTitle(re.getString("title"));
				flag=true;
			}
		}catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}
		return flag;
		}
	public static void main(String[] args){
	
	}
}